package com.example.dayday.config;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.dayday.mapper.CourseMapper;
import com.example.dayday.mapper.SpecializedMapper;
import com.example.dayday.mapper.StudentCourseMapper;
import com.example.dayday.mapper.StudentMapper;
import com.example.dayday.pojo.Course;
import com.example.dayday.pojo.Specialized;
import com.example.dayday.pojo.Student;
import com.example.dayday.pojo.StudentCourse;
import com.example.dayday.vo.StudentVo;
import org.springframework.beans.BeanUtils;

import java.util.List;

public class StudentListeren extends AnalysisEventListener<StudentVo> {

    private StudentMapper studentMapper;
    private StudentCourseMapper studentCourseMapper;
    private SpecializedMapper specializedMapper;
    private CourseMapper courseMapper;
    public StudentListeren(StudentMapper studentMapper,
                           StudentCourseMapper studentCourseMapper,
                           SpecializedMapper specializedMapper,
                           CourseMapper courseMapper) {
        this.studentMapper = studentMapper;
        this.studentCourseMapper=studentCourseMapper;
        this.specializedMapper=specializedMapper;
        this.courseMapper=courseMapper;
    }

    @Override
    public void invoke(StudentVo studentVo, AnalysisContext analysisContext) {
//        //删除

      Long studentId = studentVo.getStudentId();
        System.out.println(studentId);
        LambdaQueryWrapper<Student> s = new LambdaQueryWrapper<>();
        s.eq(Student::getStudentId,studentId);
        int delete1 = studentMapper.delete(s);
        System.out.println(delete1);
        LambdaQueryWrapper<StudentCourse> wq = new LambdaQueryWrapper<>();
        wq.eq(StudentCourse::getStudentId,studentId);
        int delete = studentCourseMapper.delete(wq);
        System.out.println(delete);

        Student student = new Student();
        BeanUtils.copyProperties(studentVo,student);
        String sex = student.getSex();
        if(sex.equals("男")){
            student.setSex("1");
        }else {
            student.setSex("2");
        }
        student.setPassword("123456");
       // student.setIsDelete("0");
        String specialized = studentVo.getSpecialized();
        System.out.println(specialized);
        LambdaQueryWrapper<Specialized> we = new LambdaQueryWrapper<>();
        we.eq(Specialized::getSpecialized,specialized);
        Specialized specialized1 = specializedMapper.selectOne(we);

                student.setClassId(specialized1.getClassId());

        studentMapper.insert(student);

        String courseName = studentVo.getCourseName();
        if(courseName!=""){
            String substring = courseName.substring(0, courseName.length() - 1);
            String[] split = substring.split(",");
            for(String ss: split){
                System.out.println(ss);
                LambdaQueryWrapper<Course> wq1 = new LambdaQueryWrapper<>();
                wq1.eq(Course::getCourseName,ss);
                Course course = courseMapper.selectOne(wq1);
                StudentCourse studentCourse = new StudentCourse();
                studentCourse.setCourseId(course.getCourseId());
                studentCourse.setStudentId(studentId);
                studentCourse.setGrades(1);
                studentCourseMapper.insert(studentCourse);
            }
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {

    }
}
